package com.video.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateConfig {
    /**
   * 指定日期加上天数后的日期
   * @param num 为增加的天数
   * @param newDate 创建时间
   * @return
   * @throws ParseException
   */
    public static String plusNewDay(int num,String newDate) throws ParseException {
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         Date currdate = format.parse(newDate);
//         System.out.println("现在的日期是：" + currdate);
         Calendar ca = Calendar.getInstance();
         ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
         currdate = ca.getTime();
         String enddate = format.format(currdate);
//         System.out.println("增加天数以后的日期：" + enddate);
         return enddate;
    }

    /**
     * 当前时间加上num天
     * @param num
     * @return
     */
    public static String plusToday(int num){
        Date d = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currdate = format.format(d);
//        System.out.println("现在的日期是：" + currdate);
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
        d = ca.getTime();
        String enddate = format.format(d);
//        System.out.println("增加天数以后的日期：" + enddate);
        return enddate;
    }

    /**
     * 将时间字符串 转换成 java.util.Date 对象
     * String input 的格式是 yyyy-MM-dd
     */
    public static Date strToDate(String time) {
        Date date = null;
        SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        try {
            date = formatter.parse(time);
        } catch ( ParseException e ) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将时间类型转换为字符串类型
     * @param date
     * @return
     */
    public static String DateToStr(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        String day = sdf.format(date);
        return day;
    }

    /**
     * 将指定时间字符串
     * @return
     */
    public static String getTimeStr(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        String dateStr = sdf.format(date);
        return dateStr;
    }

    /**
     * 获取系统当前时间字符串
     * @return
     */
    public static String getNowTimeStr(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        String dateStr = sdf.format(date);
        return dateStr;
    }

    /**
     * 比较两个时间差
     * @param startDay
     * @param lastDay
     * @return
     */
    public static long equalsDate(String startDay,String lastDay){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date beforeDay = null;
        Date afterDay = null;
        try {
            beforeDay = format.parse(startDay);
            afterDay = format.parse(lastDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long between = afterDay.getTime() - beforeDay.getTime();
        long day = between / (24 * 60 * 60 * 1000);
        return day;
    }
}
